import {
  select, call, put, all, fork, cancel, takeLatest,
} from 'redux-saga/effects'
import { delay } from 'redux-saga'
import { users as usersDuck } from 'ducks'
import QueryUsers from 'graphql/queryUsers.graphql'
import { gqlQuery } from './utils'

function* searchUser(action) {
  yield call(delay, 1500)
  if (action.value !== '') {
    yield put(usersDuck.creators.actionChangeSearchUserState('loading'))
    const result = yield call(gqlQuery, QueryUsers, { queryStr: action.value })
    yield put(usersDuck.creators.actionReciveSearchUserResults(result.QueryUsers))
  }
}

function* sortMember(action) {}

export default function* sagas() {
  yield all([takeLatest(usersDuck.types.CHANGE_SEARCH_USER_QUERY_STRING, searchUser)])
  yield all([takeLatest(usersDuck.types.CHANGE_SORT_MEMBERS_METHOD, sortMember)])
}
